package com.jingsheng.jingshengcity.mvp.ui.activity.account;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.jakewharton.rxbinding2.widget.RxTextView;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.UiUtils;
import com.jingsheng.jingshengcity.R;
import com.jingsheng.jingshengcity.di.component.DaggerVerificationCodeComponent;
import com.jingsheng.jingshengcity.di.module.VerificationCodeModule;
import com.jingsheng.jingshengcity.mvp.contract.VerificationCodeContract;
import com.jingsheng.jingshengcity.mvp.presenter.VerificationCodePresenter;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * Created by Administrator on 2017/5/12.
 */

public class VerificationCodeActivity extends BaseActivity<VerificationCodePresenter> implements VerificationCodeContract.View, View.OnClickListener {

    @NonNull
    @BindView(R.id.et_code)
    EditText mEtCode;
    @NonNull
    @BindView(R.id.btn_next)
    Button mBtnNext;

    @Override
    public void setupActivityComponent(AppComponent appComponent) {
        DaggerVerificationCodeComponent
                .builder()
                .appComponent(appComponent)
                .verificationCodeModule(new VerificationCodeModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView() {
        setTitle("手机验证码");
        return R.layout.activity_verification_code;
    }

    @Override
    public void initData() {

        Observable<CharSequence> observableCode = RxTextView.textChanges(mEtCode);

        observableCode.map(new Function<CharSequence, Boolean>() {
            @Override
            public Boolean apply(@io.reactivex.annotations.NonNull CharSequence charSequence) throws Exception {
                if (TextUtils.isEmpty(charSequence)) {
                    return false;
                }
                return true;
            }
        }).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(@io.reactivex.annotations.NonNull Boolean aBoolean) throws Exception {
                if (aBoolean) {
                    mBtnNext.setEnabled(true);
                } else {
                    mBtnNext.setEnabled(false);
                }
            }
        });


        initEvent();
    }

    private void initEvent() {
        mBtnNext.setOnClickListener(this);
    }


    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        UiUtils.SnackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        UiUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public boolean useFragment() {
        return false;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_next:
                break;
        }
    }
}